package com.android.kotlincoroutinemvvmdemo.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R

class EllipticView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            //color = context.resources.getColor(R.color.color_da8989)
            color = context.resources.getColor(R.color.color_00000000)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_B128C9)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.color_ff5722)
        }
    }

    private val mRadius = context.resources.getDimension(R.dimen.dp_5)

    private var cx = 20F
        set(value) {
            field = value
            invalidate()
        }

    private var cy = 100F
        set(value) {
            field = value
            invalidate()
        }

    private val mOvalCircle by lazy {
        RectF()
    }

    private val mPath by lazy {
        Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)

        cx = mWidth.toFloat() - 20F
        //cx = 20F
        cy = mHeight / 2.0F - 45F
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))

        canvas?.apply {
            //绘制椭圆
            mOvalCircle.set(0F + 20F, 0F + 100F , mWidth.toFloat() - 20F, mHeight.toFloat() - 100F)
            mOvalCircle.offset(0F, -45F)
            mPath.addOval(mOvalCircle, Path.Direction.CCW)
            drawPath(mPath, mPaint0)

            //绘制大圆
            drawCircle(mWidth / 2.0F, mHeight / 2.0F, mHeight / 2.0F - 45F, mPaint2)

            //绘制运动小圆
            drawCircle(cx, cy, mRadius, mPaint1)
        }
    }

    fun startAnimation() {
        val objectAnimator = ObjectAnimator.ofFloat(this, "cx", "cy", mPath)
        objectAnimator.addUpdateListener { animation ->
            val fraction = animation.animatedFraction
        }

//        objectAnimator.addListener(object : Animator.AnimatorListener {
//            override fun onAnimationStart(animation: Animator) {}
//            override fun onAnimationCancel(animation: Animator) {}
//            override fun onAnimationRepeat(animation: Animator) {}
//            override fun onAnimationEnd(animation: Animator) {
//                //Log.d(TAG,"---onAnimationEnd---")
//                //mStartAngle += floatArray[i]
//            }
//        })

        objectAnimator.duration = 5000
        //使用差值器避免中间会出现的停顿
        objectAnimator.interpolator = LinearInterpolator()
        objectAnimator.repeatMode = ValueAnimator.RESTART
        objectAnimator.repeatCount = ValueAnimator.INFINITE
        objectAnimator.start()
    }
}